﻿using EFCore.Context;
using IResoitory;
using Microsoft.EntityFrameworkCore;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Resoitory
{
    //仓储层_基类接口实现类
    public abstract class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : class, new()
    {
        //添加信息
        protected EducationContext context;
        public async Task<bool> onAdd(TEntity entity)
        {
            await context.Set<TEntity>().AddAsync(entity);
            return await context.SaveChangesAsync() > 0;
        }
        //删除信息
        public virtual async Task<bool> onDelete(int id)
        {
            //var result = await context.Set<TEntity>().FindAsync(id);
            //result.IsDelete = true;
            //return await context.SaveChangesAsync() > 0;
            return false;
        }
        //查找全部信息

        public async Task<List<TEntity>> onFindAllAsync()
        {
            return await context.Set<TEntity>().ToListAsync();
        }
        //根据ID查找信息
        //可重写
        public virtual async Task<TEntity> onFindByIdAsync(int id)
        {
            return await context.Set<TEntity>().FindAsync(id);
        }
        //更新信息
        //可重写
        public async Task<bool> onUpdate(TEntity entity)
        {
            context.Set<TEntity>().Update(entity);
            return await context.SaveChangesAsync() > 0;
        }
        //插叙单个实体对象 lambda 表达式
        public async Task<TEntity> onFindOneAsync(Expression<Func<TEntity, bool>> exp)
        {
            return await context.Set<TEntity>().Where(exp).FirstOrDefaultAsync();
        }
        //查询多个实体对象 查找条件的 lambda 表达式
        public async Task<List<TEntity>> onFindManyAsync(Expression<Func<TEntity, bool>> exp) 
        {
            return await context.Set<TEntity>().Where(exp).ToListAsync();
        }
        //登录验证
        public virtual async Task<bool> islogin(string uid, string pwd) 
        {
            return false;
        }

    }
}
